from appium import webdriver
from appium.webdriver.common.appiumby import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

des = {
    "platformName": "Android",
    "appium:platformVersion": "7.1.2",
    "appium:deviceName": "emulator-5554",
    "udid": "emulator-5554",
    "appPackage": "com.android.settings",
    "appActivity": ".Settings"
}

# 初始化驱动对象
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
time.sleep(2)       # 固定等待2秒

# # 隐式等待
# driver.implicitly_wait(20)

start_time = time.time()
try:
    # ele = driver.find_element(By.ID, "android:id/search_src_text")
    # 显式等待查找元素
    ele = WebDriverWait(driver, 10, 0.5).until(EC.presence_of_element_located((By.ID, "android:id/search_src_text")))
    ele.send_keys("蓝牙")
except:
    pass
end_time = time.time()
print("查找元素耗时：", end_time-start_time)

time.sleep(5)
driver.quit()